package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.CommonResult;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 李红磊
 * @since 2021-06-13
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;


    @ApiOperation(value = "条件查询分页方法")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public CommonResult pageCourseCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        //创建page
        Page<EduCourse> pageCondition = new Page<>(current, limit);

        //QueryWrapper,构建
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //多条件组合查询，动态sql
        String status = courseQuery.getStatus();
        String title = courseQuery.getTitle();

        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        wrapper.orderByDesc("gmt_create");

        //调用方法，实现分页查询
        IPage<EduCourse> page = eduCourseService.page(pageCondition, wrapper);

        long total = page.getTotal();//获取总记录数
        List<EduCourse> records = page.getRecords();//获取分页后的list集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        return CommonResult.ok().data("map",map);
    }


    @PostMapping("addCourseInfo")//新增课程
    public CommonResult addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        System.out.println("进入到方法");
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return CommonResult.ok().data("courseId", id);
    }



    @GetMapping()//获取所有课程列表
    public CommonResult getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return CommonResult.ok().data("list", list);
    }

    @ApiOperation(value = "根据课程id查单条")
    @GetMapping("getCourse/{id}")//根据课程id查单条
    public CommonResult getCourseId(@PathVariable("id")String courseId){
        CourseInfoVo courseInfoVo=eduCourseService.getCourseById(courseId);
        return CommonResult.ok().data("CourseInfoVo",courseInfoVo);
    }


    @PostMapping("updateCourse")//修改课程信息
    public CommonResult updateCourse(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return CommonResult.ok();
    }

    //根据课程id，查询课程确认信息
    @ApiOperation(value = "根据课程id，查询课程确认信息")
    @GetMapping("getCoursePublishInfo/{id}")
    public CommonResult getCoursePublishInfo(@PathVariable("id")String id){
        CoursePublishVo coursePublishVo=eduCourseService.coursePublishInfo(id);
        return CommonResult.ok().data("coursePublishVo",coursePublishVo);
    }


    //课程的最终发布
    @ApiOperation(value = "课程的最终发布")
    @PostMapping("publishCourse/{id}")
    public CommonResult publishCourse(@PathVariable("id")String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus("Normal");
        eduCourse.setId(id);
         boolean update = eduCourseService.updateById(eduCourse);
        return update?CommonResult.ok():CommonResult.error();
    }


    //删除课程，及其章节，小节
    @ApiOperation(value = "删除课程，及其章节，小节")
    @DeleteMapping("{courseId}")
    public CommonResult deleteIdCourse(@PathVariable("courseId")String courseId){
        eduCourseService.removeCourse(courseId);
        return CommonResult.ok();
    }

}

